package code.lucamarrocco.struts2.rest.sample;

import java.util.LinkedList;
import java.util.List;

import code.lucamarrocco.rest.RestfulMethod;
import code.lucamarrocco.rest.RestfulProtocolMather;
import code.lucamarrocco.rest.RestfulResource;

import com.opensymphony.xwork2.ModelDriven;

public class ProtocolResource extends Protocol implements ModelDriven<Object> {

	private String name;

	private String verb;

	private Object model;

	public Object getModel() {
		return model;
	}

	public String readProtocol() {
		model = this;
		return "success";
	}

	private RestfulProtocolMather mather = new RestfulProtocolMather().using(this);

	public String readProtocolMethods() {
		List<RestfulMethod> methods = new LinkedList<RestfulMethod>();
		if (verb == null) for (RestfulMethod method : getMethods()) {
			if (name != null && !method.getName().toLowerCase().contains(name)) continue;

			methods.add(method);
		}
		if (verb != null) for (RestfulMethod method : getMethods()) {
			if (name != null && !method.getName().toLowerCase().contains(name)) continue;
			if (!method.getVerb().equalsIgnoreCase(verb)) continue;

			methods.add(method);
		}
		model = methods;
		return "success";
	}

	public String readProtocolResources() {
		List<RestfulResource> resources = new LinkedList<RestfulResource>();
		for (RestfulResource resource : getResources()) {
			if (name != null && !resource.getName().toLowerCase().contains(name)) continue;
			resources.add(resource);
		}
		model = resources;
		return "success";
	}

	public void setName(String name) {
		this.name = name;
	}

	public void setVerb(String verb) {
		this.verb = verb;
	}
}
